package com.igeek.d0607;

import java.io.*;

/**
 * @Author: zhiyu
 * @Date:19:21
 * @DESC:
 */
public class Test5 {
    public static void serial(User user) {
        try (
                ObjectOutputStream oos =
                        new ObjectOutputStream(
                                new FileOutputStream(Test5.class.getResource("").getPath().substring(1)+"user.txt"));
        ){
            oos.writeObject(user);
            oos.flush();
            System.out.println("完成序列化操作");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static User unserial() {
        User user = null;
        try (
                ObjectInputStream ois =
                        new ObjectInputStream(new FileInputStream(Test5.class.getResource("").getPath().substring(1)+"user.txt"));
        ) {
            user = (User) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public static void main(String[] args) {
        User user = new User("王五", 23, new Phone("华为", "白色"));
//        serial(user);
        User unserial = unserial();
        System.out.println("unserial = " + unserial);

    }
}
